<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\service\traits;

use think\facade\Config;
use think\facade\Request;
use think\Response;
use think\response\Redirect;

trait JsonTrait
{
    private int $code = 200;

    public function code(int $code): self
    {
        $this->code = $code;
        return $this;
    }

    /**
     * 操作成功返回的数据
     * @param string $msg 提示信息
     * @param null|array $data 要返回的数据
     * @param string $type 输出类型
     * @param array $header 发送的 Header 信息
     * @return Response
     */
    public function success(string $msg = 'ok', null|array $data = [], string $type = 'json', array $header = []): Response
    {
        if (is_array($msg)) {
            $data = $msg;
            $msg = 'ok';
        }
        $this->code = 200;
        return $this->result($msg, $data, $this->code, $type, $header);
    }

    /**
     * 操作失败返回的数据1
     * @param string $msg 提示信息
     * @param null|array $data 要返回的数据
     * @param string $type 输出类型
     * @param array $header 发送的 Header 信息
     * @return Response
     */
    public function error(string $msg = 'error', null|array $data = [], string $type = 'json', array $header = []): Response
    {
        if (is_array($msg)) {
            $data = $msg;
            $msg = 'error';
        }
        $this->code = 400;
        return $this->result($msg, $data, $this->code, $type, $header);
    }

    /**
     * 返回封装后的 API 数据到客户端
     * @param $msg
     * @param array $data
     * @param int $errcode
     * @param string $type
     * @param array $header
     * @return Response
     */
    public function result($msg, array $data = [], int $errcode = 0, string $type = 'json', array $header = []): Response
    {
        $msgArray = ["ok", "error"];
        //如果消息提示为空，并且业务状态码定义了，那么就显示默认定义的消息提示
        if (!in_array($msg, $msgArray) && !empty(config("status." . $errcode))) {
            $msg = config("status." . $errcode);
        }
        $result = [
            'code' => $this->code == 200 ? $this->code : $errcode,
            'msg' => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];

        // 如果未设置类型则自动判断
        $type = Config::get("route.default_jsonp_handler");

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断$code = $code >= 1000 || $code < 200 ? 200 : $code;
            $code = $errcode >= 1000 || $this->code < 200 ? 200 : $this->code;
        }
        return Response::create($result, $type, $code)->header($header);
    }

    /**
     * @param string $url
     * @param array $vars
     * @param int $code
     * @param $suffix
     * @param $domain
     * @return Redirect
     */
    public function redirect(string $url = '', array $vars = [], int $code = 301, $suffix = false, $domain = false): Redirect
    {
        $header = [];
        if (isset($vars['header'])) {
            $header = $vars['header'];
            unset($vars['header']);
        }
        if(strpos($url, '://')) $response = redirect($url, $code)->header($header);
        else $response = redirect((string)url($url, $vars, $suffix, $domain), $code)->header($header);
        //throw new HttpResponseException($response);
        return $response;
    }
}